package com.xhtech.exercise.ui

import android.content.Intent
import android.os.Bundle
import com.gyf.immersionbar.ImmersionBar
import me.luochenyxh.base.base.BaseActivity
import com.xhtech.exercise.ui.ac.MainActivity
import com.xhtech.exercise.databinding.ActivityWelcomeBinding
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.luochenyxh.base.util.CacheUtil
import me.hgj.jetpackmvvm.ext.view.gone
import me.hgj.jetpackmvvm.ext.view.visible


/**
 */
@Suppress("DEPRECATED_IDENTITY_EQUALS")
class WelcomeActivity : BaseActivity<BaseViewModel, ActivityWelcomeBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        //防止出现按Home键回到桌面时，再次点击重新进入该界面bug
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT !== 0) {
            finish()
            return
        }

        ImmersionBar.with(this).init()

        mDatabind.click = ProxyClick()
        mDatabind.run {
            if (CacheUtil.isFirst()) {
                //是第一次打开App 显示引导页
                welcomeImage.gone()
                image.postDelayed({
                    startActivity(Intent(this@WelcomeActivity, MainActivity::class.java))
                    finish()
                    //带点渐变动画
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                }, 300)
            } else {
                //不是第一次打开App 0.3秒后自动跳转到主页
                welcomeImage.visible()
                welcomeImage.postDelayed({
                    startActivity(Intent(this@WelcomeActivity, MainActivity::class.java))
                    finish()
                    //带点渐变动画
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                }, 300)
            }
        }
    }

    inner class ProxyClick {
        fun toMain() {
            CacheUtil.setFirst(false)
            startActivity(Intent(this@WelcomeActivity, MainActivity::class.java))
            finish()
            //带点渐变动画
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        }
    }

}